package cn.jcet.mapreduce.outputformat;

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;

/**
 * @author 鲜磊
 * @Date 2020/12/15 11:34
 */
public class FileRecordWriter extends RecordWriter<Text,NullWritable> {

    private FSDataOutputStream fosatguigu;
    private FSDataOutputStream fosother;
    public FileRecordWriter(TaskAttemptContext job) {
        try {
            // 获取文件系统
            FileSystem fs = FileSystem.get(job.getConfiguration());
            // 创建atguigu.log输出流
            fosatguigu = fs.create(new Path("e:/code/atguigu.log"));
            // 创建other.log输出流
            fosother = fs.create(new Path("e:/code/other.log"));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void write(Text text, NullWritable nullWritable) throws IOException, InterruptedException {
        // 转换类型
        String word = text.toString();
        // 是否包含atguigu
        if (word.contains("atguigu")){
            fosatguigu.write(word.getBytes());
        }else {
            fosother.write(word.getBytes());
        }
    }

    @Override
    public void close(TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {
        // 关闭资源
        IOUtils.closeStream(fosatguigu);
        IOUtils.closeStream(fosother);
    }
}
